package com.jae.security.filter;


import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.oauth2.common.exceptions.BadClientCredentialsException;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter;
import org.springframework.security.web.AuthenticationEntryPoint;

public class OauthServerClientCredentialsTokenEndpointFilter extends ClientCredentialsTokenEndpointFilter {
    private AuthenticationEntryPoint authenticationEntryPoint;
    private final AuthorizationServerSecurityConfigurer configurer;


    public OauthServerClientCredentialsTokenEndpointFilter(AuthorizationServerSecurityConfigurer configurer,AuthenticationEntryPoint authenticationEntryPoint) {
        this.authenticationEntryPoint = authenticationEntryPoint;
        this.configurer = configurer;
    }

    @Override
    public void setAuthenticationEntryPoint(AuthenticationEntryPoint authenticationEntryPoint) {
        this.authenticationEntryPoint = authenticationEntryPoint;
    }

    @Override
    protected AuthenticationManager getAuthenticationManager() {
        return configurer.and().getSharedObject(AuthenticationManager.class);
    }

    @Override
    public void afterPropertiesSet() {
        setAuthenticationFailureHandler((request,response,exception) -> {
            if(exception instanceof BadCredentialsException){
                exception = new BadCredentialsException(exception.getMessage(),new BadClientCredentialsException());
            }
            authenticationEntryPoint.commence(request,response,exception);
        });
        setAuthenticationSuccessHandler((request,response,exception) -> {

        });
    }
}
